package com.moli.iov.admin.service.impl;

import com.moli.iov.admin.constants.Constants;
import com.moli.iov.admin.model.vo.req.CompanyVo;
import com.moli.iov.admin.service.ICompanyFacadeService;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.sys.model.dto.req.CompanyDto;
import com.moli.iov.sys.model.po.Company;
import com.moli.iov.sys.remote.ICompanyRemoteService;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.ExcelUtil;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 企业管理 业务
 * </p>
 *
 * @author zql
 * @since 2019/9/4 16:00
 */
@Slf4j
@Service
public class CompanyFacadeServiceImpl implements ICompanyFacadeService {

  @Reference(group = "${service.group}", check = false)
  private ICompanyRemoteService companyRemoteService;

  /**
   *
   * 分页查询企业列表
   *
   * @param companyVo
   * @return
   */
  @Override
  public RestResponse<PageExtend<Company>> listCompanyOfPage(CompanyVo companyVo) {
    return companyRemoteService.listCompanyOfPage(convert(companyVo));
  }

  /**
   * 查询企业列表  -- 下拉框
   * @param companyVo
   * @return
   */
  @Override
  public RestResponse<List<Company>> listCompany(CompanyVo companyVo) {
    return companyRemoteService.listCompany(convert(companyVo));
  }

  /**
   * 增加企业
   * @param companyVo
   * @return
   */
  @Override
  public RestResponse saveCompany(CompanyVo companyVo) {
    return companyRemoteService.saveCompany(convert(companyVo));
  }

  /**
   * 修改企业
   * @param companyVo
   * @return
   */
  @Override
  public RestResponse updateCompanyByCompanyId(CompanyVo companyVo) {
    RestResponse restResponse = companyRemoteService.updateCompanyByCompanyId(convert(companyVo));
    return restResponse;
  }

  /**
   * 获取单个企业
   * @param companyVo
   * @return
   */
  @Override
  public RestResponse getCompanyByCompanyId(CompanyVo companyVo) {
    return companyRemoteService.getCompanyByCompanyId(convert(companyVo));
  }

  /**
   * 转换成 CompanyDto
   * @param companyVo
   * @return
   */
  private CompanyDto convert(CompanyVo companyVo){
    CompanyDto companyDto = new CompanyDto();
    CommonUtils.copyProperties(companyVo, companyDto);
    companyDto.setOperatorId(companyVo.getSysUserId());
    companyDto.setOperatorName(companyVo.getSysRealName());
    return companyDto;
  }

  /**
   * 导出
   * @param companyVo
   * @param request
   * @param res
   */
  @Override
  public void exportCompany(CompanyVo companyVo, HttpServletRequest request,
      HttpServletResponse res) throws Exception {
    companyVo.setPageNum(Constants.EXPORT_PAGE_NUM);
    companyVo.setPageSize(Constants.EXPORT_PAGE_SIZE);
    RestResponse<PageExtend<Company>> pageExtendRestResponse = companyRemoteService
        .listCompanyOfPage(convert(companyVo));
    if(!pageExtendRestResponse.flag || pageExtendRestResponse.getData() == null){
      log.info("导出失败");
      return;
    }

    final String[] propNames = {"companyName","provinceName", "cityName", "address"
        ,"corporationName", "corporationPhone","statusStr","createTime"};
    final String[] headStrs = {"公司名称","省", "市","详细地址", "负责人姓名","负责人手机号","状态","录入时间"};
    String title = "企业管理";
    List<Company> list = pageExtendRestResponse.getData().getRecords();
    ExcelUtil.exportDataExcel(request, res, list, propNames, headStrs, title);
  }
}
